package cn.swu.cs.lesson12

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    val TAG = "@@MainActivity"

    lateinit var btn: Button
    lateinit var bar: ProgressBar
    lateinit var per: TextView

    private var mBinder: DownloadService.DownloadBinder? = null

    private var serviceConnect = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            mBinder = service as DownloadService.DownloadBinder
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            mBinder = null
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        bar = findViewById(R.id.progress)
        per = findViewById(R.id.txt_progress)
        findViewById<Button>(R.id.btn_webview).setOnClickListener {
            navToWebview()
        }
        btn = findViewById<Button?>(R.id.btn_download).apply {
            this.setOnClickListener {
                if (btn.text == "开始下载") {
                    btn.text = "停止下载"
                    startDownload()
                } else {
                    btn.text = "开始下载"
                    stopDownload()
                }
            }
        }

        connectService()

    }

    private fun navToWebview() {
        startActivity(Intent(this,WebviewActivity::class.java))
    }

    private fun connectService() {
        val intent = Intent(this,DownloadService::class.java)
        bindService(intent,serviceConnect, Context.BIND_AUTO_CREATE)
//        startService(intent)
    }

    private fun stopDownload() {
        mBinder?.stopDownload()
    }

    private fun startDownload() {
        mBinder?.startDownload(object :DownloadService.DownloadListener{
            override fun onStart() {
                bar.post {
                    bar.visibility = View.VISIBLE
                }
            }

            override fun onProgress(total: Int, current: Int) {
                bar.post {
                    bar.max = total
                    bar.progress = current
                    val perc = ((current.toDouble() / total) * 100) .toString()
                    Log.d(TAG,perc)
                    per.text = "${perc}%"
                }
            }

            override fun onFinished() {
                bar.post {
                    bar.visibility = View.GONE
                }
            }

            override fun onStop() {
                bar.visibility = View.GONE
            }

        })
    }
}